using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Entity;
using Newtonsoft.Json;

namespace ApiProject.Api.Uitls
{
    public class ArrangeHelper
    {
        public static List<CommodityClassifications> GetMenu(int pid, List<CommodityClassifications> demos = null)
        {
            /*
             思路：1.从数据源中找到父级
                   2.循环父级并赋值，再循环父级时查找子集
                   3.如果有子集调用用GetMenu（父级Id,数据源）方法一层一层向下找
                   4.注意：（，是套娃模式。也就是循环第二层第三层的时候还是在一个父
                   级下面）个人注意点因为我在这里差点没想通，总是想着是平级
             */
            var parent = demos.Where(P => P.ParentLevelId == pid);
            List<CommodityClassifications> lists = new List<CommodityClassifications>();
            foreach (var item in parent)
            {

                CommodityClassifications DemosChilder = new CommodityClassifications();
                DemosChilder.ClassificationName = item.ClassificationName;
                DemosChilder.Id = item.Id;
                DemosChilder.ClassLevel = item.ClassLevel;
                DemosChilder.Children = GetSon(DemosChilder, demos);
                lists.Add(DemosChilder);
            }

            /// <summary>
            /// 找子集有就返回NUll，并执行Add
            /// </summary>
            /// <param name="demos">父级Id</param>
            /// <param name="demosd">数据源</param>
            /// <returns></returns>
            static List<CommodityClassifications> GetSon(CommodityClassifications demos, List<CommodityClassifications> demosd = null)
            {

                return GetMenu(demos.Id, demosd);

            }
            return lists;
        }

    }
}